import {createRouter, createWebHistory} from 'vue-router'
import type {RouteRecordRaw} from 'vue-router'
import Login from '@/views/login/index.vue'
import Github from '@/views/github/index.vue'
import Dashboard from '@/views/dashboard/index.vue'
import {getStorage, USER_TOKEN} from "@/utils/localStorage";
import {Message} from "@arco-design/web-vue";


const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        redirect: '/dashboard'
    },

    {
        path: '/login',
        component: Login,
    },
    {
        path: '/github',
        component: Github
    },
    {
        path: '/dashboard',
        component: Dashboard
    }
]


const router = createRouter({
    history: createWebHistory(''),
    routes: routes,
})

const whiteList = ['/login','/github']
router.beforeEach((to, __, next) => {
    const token = getStorage(USER_TOKEN);
    if (token || whiteList.includes(to.path)) {
        next();
    } else {
        Message.error('登录过期 请重新登录')
        next('/login')
    }
})


export default router;